package com.patMall.service.impl;

import com.patMall.entity.Comment;
import com.patMall.entity.CommentImg;
import com.patMall.entity.Commodity;
import com.patMall.entity.User;
import com.patMall.mapper.CommentImgMapper;
import com.patMall.mapper.CommentMapper;
import com.patMall.mapper.CommodityMapper;
import com.patMall.mapper.UserMapper;
import com.patMall.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.service.impl
 * @Date 2024/2/18 10:39
 * @Version 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private CommentImgMapper commentImgMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommodityMapper commodityMapper;
    @Override
    public int addComment(Comment comment) {
        return commentMapper.insertComment(comment);
    }

    @Override
    public int updateComment(Comment comment) {
        return commentMapper.updateComment(comment);
    }

    @Override
    public List<Comment> getComments(Comment comment) {
        return commentMapper.selectComment(comment);
    }

    @Override
    public List<Comment> findAllComment() {
        List<Comment> commentList = commentMapper.findAllComment();
        for (Comment comment : commentList) {
            List<CommentImg> commentImgList = commentImgMapper.selectCommentImgList(comment.getCommentId());
            comment.setCommentImgList(commentImgList);
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            comment.setUser(user);
            Commodity commodity = commodityMapper.getCommodityId(comment.getCommodityId());
            comment.setCommodity(commodity);
        }
        return commentList;
    }

    @Override
    public List<Comment> getAllByCommentContent(String commentContent) {
        List<Comment> commentList = commentMapper.getAllByCommentContent(commentContent);
        for (Comment comment : commentList) {
            List<CommentImg> commentImgList = commentImgMapper.selectCommentImgList(comment.getCommentId());
            comment.setCommentImgList(commentImgList);
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            comment.setUser(user);
            Commodity commodity = commodityMapper.getCommodityId(comment.getCommodityId());
            comment.setCommodity(commodity);
        }
        return commentList;
    }
}
